package com.apes.hr.recruit.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.base.model.Staffing;
import com.apes.hr.base.repository.StaffingRepository;
import com.apes.hr.base.service.StaffingService;
import com.apes.hr.recruit.model.RecruitOrder;
import com.apes.hr.recruit.repository.RecruitOrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能：招聘申请
 *
 * @author HuJianLin
 * @create 2018-04-28 14:35
 */
@Service("hrRecruitOrderService")
public class RecruitOrderService extends DomainService {
    @Autowired
    public RecruitOrderRepository recruitOrderRepository;
    @Autowired
    public StaffingRepository staffingRepository;
    @Autowired
    public StaffingService staffingService;

    public RecruitOrder save(SimpleRequest request) {
        RecruitOrder recruitOrder = request.getO(RecruitOrder.class);
        recruitOrder.setState("create");
        return recruitOrderRepository.saveAndFlush(recruitOrder);
    }

    /**
     * 检查数据判断，判读当前的输入版本信息
     *
     * @param request
     */
    public void checkData(SimpleRequest request) {
        String id = request.get("id");
        int version = request.get("version");

        if (request.get("staffing") == null) {
            throw new RuntimeException("人员编制不能为空!");
        }

        // 从数据库中取数，检查数据
        RecruitOrder oldRecruitOrder = recruitOrderRepository.findOne(id);
        if (oldRecruitOrder == null) throw new RuntimeException("招聘申请单【" + id + "】在系统中不存在。");
        if (oldRecruitOrder.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");
    }

    public RecruitOrder update(SimpleRequest request) {
        // 检查数据
        checkData(request);

        RecruitOrder recruitOrder = request.getO(RecruitOrder.class);
        return recruitOrderRepository.saveAndFlush(recruitOrder);
    }


    /**
     * 功能：审核招聘申请
     *
     * @param request
     * @return
     */
    public RecruitOrder approve(SimpleRequest request) {
        // 检查数据
        checkData(request);

        // 审核
        RecruitOrder recruitOrder = request.getO(RecruitOrder.class);
        recruitOrder.setApproveDate(new Date());
        recruitOrder.setApproveUid(getPerson(request));
        recruitOrder.setState("approve");

        //更新人员编制
        Staffing staffing = staffingRepository.findOne(recruitOrder.getStaffing().getId());

        // 人员编制-申请招聘人数 + 招聘申请单-申请招聘人数
        staffing.setRecruitQty(staffing.getRecruitQty() + recruitOrder.getCurrentRecruitQty());
        staffingService.create(staffing);

        return recruitOrderRepository.saveAndFlush(recruitOrder);
    }

    public RecruitOrder findOne(String id) {
        return recruitOrderRepository.findOne(id);
    }


    public Page<RecruitOrder> findByState(SimpleRequest request) {
        String id = request.get("key");
        List<Map> filters = new ArrayList<>();
        if ("".equals(id) || id.isEmpty()) {
            JSONObject oKey = new JSONObject();
            oKey.put("field", "id");
            oKey.put("operator", "LIKE");
            oKey.put("value", id);
            filters.add(oKey);
        }
        JSONObject state = new JSONObject();
        state.put("field", "state");
        state.put("operator", "EQ");
        state.put("value", "approve");
        filters.add(state);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return recruitOrderRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size"), sort));
    }
}
